package com.solotiger.douban.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.solotiger.douban.MyApplication;
import com.solotiger.douban.R;
import com.solotiger.douban.biz.model.movie.Cast;
import com.solotiger.douban.biz.model.movie.Subject;
import com.solotiger.douban.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * MovieTopAdapter
 */
public class MovieTopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final LayoutInflater mInflater;

    private List<Subject> mMovieList;

    public MovieTopAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        mMovieList = new ArrayList<>();
    }

    public void notifyDataSetChanged(List<Subject> movieList) {
        if (movieList != null) {
            this.mMovieList.addAll(movieList);
        }
        super.notifyDataSetChanged();
    }

    public void clear() {
        this.mMovieList.clear();
        super.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new TopViewHolder(mInflater.inflate(R.layout.item_movie_top, parent, false),
                null);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TopViewHolder comingViewHolder = (TopViewHolder) holder;
        Subject subject = mMovieList.get(position);
        comingViewHolder.setData(subject, position);
    }

    @Override
    public int getItemCount() {
        return mMovieList == null ? 0 : mMovieList.size();
    }

    private static class TopViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final OnItemClickListener mItemClickListener;

        private SimpleDraweeView mIvImage;
        private TextView mTitle;
        private TextView mRating;
        private TextView mNo;
        private TextView mCasts;
        private TextView mGenres;
        private TextView mRateNum;
        private RatingBar mRatingBar;

        TopViewHolder(View itemView, OnItemClickListener itemClickListener) {
            super(itemView);

            mIvImage = (SimpleDraweeView) itemView.findViewById(R.id.image);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
            mRating = (TextView) itemView.findViewById(R.id.rating);
            mNo = (TextView) itemView.findViewById(R.id.no);
            mCasts = (TextView) itemView.findViewById(R.id.casts);
            mGenres = (TextView) itemView.findViewById(R.id.genres);
            mRateNum = (TextView) itemView.findViewById(R.id.rate_num);

            this.mItemClickListener = itemClickListener;

            itemView.setOnClickListener(this);
        }

        void setData(Subject subject, int position) {
            Uri uri = Uri.parse(subject.getImages().getMedium());
            mIvImage.setImageURI(uri);
            mTitle.setText(subject.getTitle());
            mRatingBar.setRating((float) subject.getRating().getAverage() / 2);
            mRating.setText(String.valueOf(subject.getRating().getAverage()));
            mRateNum.setText(MyApplication.getInstance().getString(R.string.txt_rate_count, String.valueOf(subject.getCollect_count())));
            mNo.setText("NO." + (position + 1));

            if (position == 0) {
                mNo.setBackgroundResource(R.drawable.txt_no_1_bg);
            } else if (position == 1) {
                mNo.setBackgroundResource(R.drawable.txt_no_2_bg);
            } else if (position == 2) {
                mNo.setBackgroundResource(R.drawable.txt_no_3_bg);
            } else {
                mNo.setBackgroundResource(R.drawable.txt_no_other_bg);
            }

            StringBuffer stringBuffer = new StringBuffer();
            for (String str : subject.getGenres()) {
                stringBuffer.append(str + " ");
            }
            mGenres.setText(stringBuffer.toString());

            stringBuffer = new StringBuffer();
            for (Cast cast : subject.getCasts()) {
                stringBuffer.append(cast.getName() + "/");
            }
            mCasts.setText(stringBuffer.substring(0, stringBuffer.length() - 1));

        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null && v == itemView) {
                mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
